package cn.restcloud.example.controller.apm;

import org.bson.Document;

import cn.restcloud.framework.client.RestClient;
import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.MethodType;
import cn.restcloud.framework.core.annotation.rest.Param;
import cn.restcloud.framework.core.annotation.rest.Params;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.view.base.DocAndView;
import cn.restcloud.framework.view.util.ViewUtil;

@BeanConfig(appId = "example",beanId = "ApmQueryUserInfoRest",beanName = "查询会员信息APM示例",beanType = BeanType.CONTROLLER)
@RestConfig(url = "/rest/example/apm")
public class ApmQueryUserInfoRest {

	private String hostUrl="http://localhost:8080/restcloud";
	
	@RestConfig(configName = "查询会员信息显示APM链路(入口API)",method = MethodType.GET,url = "/getUserInfo")
	@Params({ 
		@Param(id = "userId", name = "用户id",required=true),
	})
	public DocAndView getUserInfo(String userId) throws Exception{
		//通过测试本API可以形成一个APM跟踪链路在日记中可以查看
		String url=hostUrl+"/rest/example/apm/"+userId;
		Document doc=RestClient.getInstance().setUrl(url).get().getDocument();
		return ViewUtil.getJsonView(doc);
	}
	
	@RestConfig(configName = "获取会员积分和地址",method = MethodType.GET,url = "/{userId}")
	@Params({ 
		@Param(id = "userId", name = "用户id",required=true),
	})
	public DocAndView getUserId(String userId) throws Exception{
		Document countDoc=RestClient.getInstance().setUrl(hostUrl+"/rest/example/apm/getcount").addUrlParams("userId",userId).get().getDocument();
		Document addressDoc=RestClient.getInstance().setUrl(hostUrl+"/rest/example/apm/getaddress").addUrlParams("userId",userId).get().getDocument();
		Document userDoc=new Document();
		userDoc.put("count", countDoc);
		userDoc.put("address", addressDoc);
		return ViewUtil.getJsonView(userDoc);
	}
	
	@RestConfig(configName = "获取会员积分",method = MethodType.GET,url = "/getcount")
	@Params({ 
		@Param(id = "userId", name = "用户id",required=true),
	})
	public DocAndView getUserCount(String userId) throws Exception{
		return ViewUtil.getSuccessView("会员("+userId+")的积分为100分");
	}
	
	@RestConfig(configName = "获取会员地址",method = MethodType.GET,url = "/getaddress")
	@Params({ 
		@Param(id = "userId", name = "用户id",required=true),
	})
	public DocAndView getUserAddress(String userId) throws Exception{
		return ViewUtil.getSuccessView("会员("+userId+")的地址为广州市天河区");
	}
	
}
